package com.haixiaoke.saas.repository.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.repository.mapper.ApartmentLabelMapper;
import com.haixiaoke.saas.repository.pojo.ApartmentLabel;
import com.haixiaoke.saas.repository.service.IApartmentLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 公寓标签Service业务层处理
 *
 * @author qixi
 * @date 2023-05-11
 */
@Service
public class ApartmentLabelServiceImpl implements IApartmentLabelService {
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;

    /**
     * 查询公寓标签
     *
     * @param labelId 公寓标签主键
     * @return 公寓标签
     */
    @Override
    public ApartmentLabel selectApartmentLabelByLabelId(Long labelId) {
        return apartmentLabelMapper.selectApartmentLabelByLabelId(labelId);
    }

    /**
     * 查询公寓标签列表
     *
     * @param apartmentLabel 公寓标签
     * @return 公寓标签
     */
    @Override
    public List<ApartmentLabel> selectApartmentLabelList(ApartmentLabel apartmentLabel) {
        return apartmentLabelMapper.selectApartmentLabelList(apartmentLabel);
    }

    /**
     * 新增公寓标签
     *
     * @param apartmentLabel 公寓标签
     * @return 结果
     */
    @Override
    public int insertApartmentLabel(ApartmentLabel apartmentLabel) {
        apartmentLabel.setCreateTime(new Date());
        return apartmentLabelMapper.insertApartmentLabel(apartmentLabel);
    }

    /**
     * 修改公寓标签
     *
     * @param apartmentLabel 公寓标签
     * @return 结果
     */
    @Override
    public int updateApartmentLabel(ApartmentLabel apartmentLabel) {
        apartmentLabel.setUpdateTime(new Date());
        return apartmentLabelMapper.updateApartmentLabel(apartmentLabel);
    }

    /**
     * 批量删除公寓标签
     *
     * @param labelIds 需要删除的公寓标签主键
     * @return 结果
     */
    @Override
    public int deleteApartmentLabelByLabelIds(Long[] labelIds) {
        return apartmentLabelMapper.deleteApartmentLabelByLabelIds(labelIds);
    }

    /**
     * 删除公寓标签信息
     *
     * @param labelId 公寓标签主键
     * @return 结果
     */
    @Override
    public int deleteApartmentLabelByLabelId(Long labelId) {
        return apartmentLabelMapper.deleteApartmentLabelByLabelId(labelId);
    }

    @Override
    public List<ApartmentLabel> selectApartmentLabelByOrgIdId(Long orgId) {
        return apartmentLabelMapper.selectApartmentLabelByOrgIdId(orgId);
    }

    @Override
    public String selectApartmentLabelNamesByLabelIdIn(List<Long> labelIds) {
        List<ApartmentLabel> labelList = this.selectApartmentLabelByLabelIdIn(labelIds);
        StringBuilder labelNames = new StringBuilder();
        if (CollectionUtil.isNotEmpty(labelList)){
            labelList.forEach(m->labelNames.append(m.getLabelName()).append(','));
        }
        if (StrUtil.isNotBlank(labelNames)) {
            labelNames.deleteCharAt(labelNames.lastIndexOf(","));
        }
        return labelNames.toString();
    }

    @Override
    public List<ApartmentLabel> selectApartmentLabelByLabelIdIn(List<Long> labelIds) {
        return apartmentLabelMapper.selectApartmentLabelByLabelIdIn(labelIds);
    }

    @Override
    public boolean checkOrgIdLabelNameUnique(Long orgId, String labelName) {
        return apartmentLabelMapper.checkOrgIdLabelNameUnique(orgId,labelName) == 0;
    }
}
